/**
 * 
 */
package com.kavitha.normal.web;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @author Vijayan Srinivasan
 * @since May 1, 2013 3:40:16 PM
 * 
 */
public class MyFirstServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private Log log = LogFactory.getLog(getClass());

	public MyFirstServlet() {
		super();
		log.info("MyFirstServlet() called");
	}

	@Override
	public void init() throws ServletException {
		log.info("Init() called");
		super.init();
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// log.info("doGet() called");
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// log.info("doPost() called");
		printRequestParams(req);
		String action = req.getParameter("action");

		String aS = req.getParameter("a");
		String bS = req.getParameter("b");

		if ("add".equalsIgnoreCase(action)) {
			req.setAttribute("result",
					(Double.valueOf(aS) + Double.valueOf(bS)));
		}else if("sub".equalsIgnoreCase(action)){
			req.setAttribute("result",
					(Double.valueOf(aS) - Double.valueOf(bS)));
		}

		RequestDispatcher dispatcher = req.getRequestDispatcher("index.jsp");
		dispatcher.forward(req, resp);
	}

	private void printRequestParams(HttpServletRequest req) {
		Enumeration<?> names = req.getParameterNames();
		while (names.hasMoreElements()) {
			String name = (String) names.nextElement();
			log.info(name + "=" + req.getParameter(name));
		}
	}

	/*
	 * @Override protected void doPut(HttpServletRequest req,
	 * HttpServletResponse resp) throws ServletException, IOException {
	 * log.info("doPut() called"); printRequestParams(req); }
	 * 
	 * @Override protected void doDelete(HttpServletRequest req,
	 * HttpServletResponse resp) throws ServletException, IOException {
	 * log.info("doDelete() called"); printRequestParams(req); }
	 * 
	 * @Override protected void doHead(HttpServletRequest req,
	 * HttpServletResponse resp) throws ServletException, IOException {
	 * log.info("doHead() called"); printRequestParams(req); }
	 * 
	 * @Override protected void doTrace(HttpServletRequest req,
	 * HttpServletResponse res) throws ServletException, IOException {
	 * log.info("doTrace() called"); printRequestParams(req); }
	 * 
	 * @Override protected void doOptions(HttpServletRequest req,
	 * HttpServletResponse res) throws ServletException, IOException {
	 * log.info("doOptions() called"); printRequestParams(req); }
	 */

	@Override
	public void destroy() {
		log.info("destroy() called");
	}

}
